/*
 *  Projectile.cpp
 *  GLRender
 *
 *  Created by Coloss on 5/19/10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "Projectile.h"
#include "Player.h"
#include "SoundEngine.h"
#include "Physics.h"

static const double PROJECTIE_SPEED = 8.0, STARTING_DISTANCE = 3.0;

Projectile::Projectile (Player *p, const double strength )
	: Weapon(strength, 
			 100.0, 
			 p->velocity()+Point3d(PROJECTIE_SPEED*p->velocity().x()/p->velocity().norm(),PROJECTIE_SPEED*p->velocity().y()/p->velocity().norm(),PROJECTIE_SPEED*p->velocity().z()/p->velocity().norm()) , 
			 p->position() + Point3d(STARTING_DISTANCE*p->velocity().x()/p->velocity().norm(),STARTING_DISTANCE*p->velocity().y()/p->velocity().norm(),STARTING_DISTANCE*p->velocity().z()/p->velocity().norm()))
{
	theta = 20;
	angle = 0;
	reaction.addReaction("SpaceShip",IGNORE);
	reaction.addReaction("Projectile",IGNORE);
	reaction.addReaction("Asteroid",HARM);
}

Weapon * Projectile::generate (Player *p) 
{ 
	SoundEngine::getInstance()->play("Projectile");
	return new Projectile(p, getStrength()); 
}

void Projectile::renderOn (AbstractRenderer *renderer) 
{ 
	renderer->renderProjectile(this); 
}


void Projectile::onCollision (WObject *from)
{
	switch (from->getReaction().getReaction("Projectile")) {
		case DIE:
			this->alive() = false;
			break;
		case HARM:
			this->life -= from->getStrength();
			if (this->life <= 0)
				this->alive() = false;
			break;
		default:
			Physics::getInstance()->collide(this, from);
			collided = true;
			break;
	}
}

void Projectile::step (double tau)
{
	WMovingObject::step(tau);
	angle += theta*tau;
	lifeDone += tau;
	if (lifeDone > maxLife)
		this->alive() = false;
}